﻿using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using WroxBlog.Controllers;
using WroxBlog.Models;
using WroxBlog.Models.Interfaces;

namespace WroxBlog.Tests
{
    [TestClass()]
    public class PostControllerTest
    {
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        private MockRepository mocks;

        private ICategoryRepository categoryMockContext;
        private IPostRepository postMockContext;
        private ICommentRepository commentMockContext;

        [TestInitialize]
        public void SetupMocks()
        {
            mocks = new MockRepository();
            categoryMockContext = mocks.DynamicMock<ICategoryRepository>();
            postMockContext = mocks.DynamicMock<IPostRepository>();
            commentMockContext = mocks.DynamicMock<ICommentRepository>();
        }

        [TestMethod()]
        public void IndividualPostTest()
        {
            CategoryCollection fakeCategories = new CategoryCollection();
            for (int i = 0; i < 5; i++)
            {
                fakeCategories.Add(new Category() { ID = i });
            }

            Post fakePost = new Post()
            {
                ID = 6,
                Title = "Test",
                Body = "This is a test post"
            };

            CommentCollection fakeComments = new CommentCollection();
            for (int i = 0; i < 3; i++)
            {
                fakeComments.Add(new Comment()
                {
                    ID = i
                });
            }

            using (mocks.Record())
            {
                Expect.Call(categoryMockContext.GetCategories()).Return(fakeCategories);
                Expect.Call(postMockContext.GetPost(6)).Return(fakePost);
                Expect.Call(commentMockContext.GetComments(6)).Return(fakeComments);
            }

            PostController controller = new PostController
                (postMockContext, categoryMockContext, commentMockContext);

            using (mocks.Playback())
            {
                ActionResult result = controller.IndividualPost(6);
                ViewResult viewResult = result as ViewResult;

                string title = (string)viewResult.ViewData["Title"];
                Assert.AreEqual("Test", title);

                CategoryCollection categories = viewResult.ViewData["Categories"]
                    as CategoryCollection;
                Assert.AreEqual(5, categories.Count);

                Post post = viewResult.ViewData["Post"] as Post;
                Assert.AreEqual("This is a test post", post.Body);

                CommentCollection comments = viewResult.ViewData["Comments"]
                    as CommentCollection;
                Assert.AreEqual(3, comments.Count);
            }
        }
    }
}
